package com.bbdjt.weixin.web.action.syllabus;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.rubyeye.xmemcached.MemcachedClient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bbdjt.weixin.base.action.BaseAction;
import com.bbdjt.weixin.model.Bespoke;
import com.bbdjt.weixin.model.Syllabus;
import com.bbdjt.weixin.model.SyllabusQueryModel;
import com.bbdjt.weixin.service.BespokeService;
import com.bbdjt.weixin.service.SyllabusService;
import com.github.pagehelper.PageInfo;

/**
 * @项目名称: bbdjt
 * @类名称: SyllabusAction
 * @类描述: Syllabus表相关操作
 * @创建人: guoLiang
 * @创建时间: 2017-01-18 04:37:46
 * @修改人: 
 * @修改时间: 
 * @修改备注:
 * @version
 */
@Controller
public class SyllabusAction extends BaseAction {
	
	@Autowired
	private SyllabusService syllabusService;
	
	@Autowired
	private BespokeService bespokeService;
	
	@Autowired
	private MemcachedClient memcachedClient;

	private String page_list = "jsp/syllabus/syllabusindex";

	private String redirect_list = "redirect:r_syllabus";
	
	
	// 分页查询
	@RequestMapping(value = "r_syllabus")
	public ModelAndView getListPage(SyllabusQueryModel syllabus) {
		ModelAndView result = new ModelAndView(page_list);
		syllabus.setOrderBy("id asc");
		List<Syllabus> syllabusList = syllabusService.selectBySyllabus(syllabus);
		result.addObject("pageInfo", new PageInfo<Syllabus>(syllabusList));
		result.addObject("syllabus", syllabus);
		return result;
	}

	// 自定义分页查询 根据xml 分页查询
	@RequestMapping(value = "r_syllabusXml")
	public ModelAndView getListPageXml(SyllabusQueryModel syllabus) {
		ModelAndView result = new ModelAndView(page_list);
		syllabus.setOrderBy("id asc");
		PageInfo<SyllabusQueryModel> pageInfo = syllabusService.selectBySyllabusQueryModel(syllabus);
		List<SyllabusQueryModel> syllabusList = pageInfo.getList();
		result.addObject("pageInfo", new PageInfo<SyllabusQueryModel>(syllabusList));
		result.addObject("syllabus", syllabus);
		return result;
	}

	// 返回整个页面的 json 包括分页 也签 内容 标示 和 正文 内容 同上方法 只不过是返回json
	@RequestMapping(value = "r_syllabusJson", produces = "application/json")
	public ModelAndView pageInfo(SyllabusQueryModel syllabus) {
		ModelAndView result = new ModelAndView();
		PageInfo<SyllabusQueryModel> pageInfo = syllabusService.selectBySyllabusQueryModel(syllabus);
		result.addObject("pageInfo", pageInfo);
		return result;
	}
	
	//ligerui table
	@ResponseBody
	@RequestMapping(value = "r_syllabusLigerUi", produces="application/json;charset=UTF-8") 
	public String pageInfoLigerUi(SyllabusQueryModel syllabus) {
		syllabus.setOrderBy(syllabus.getSortname(),syllabus.getSortorder());
		PageInfo<SyllabusQueryModel> pageInfo = syllabusService.selectBySyllabusQueryModel(syllabus);
		return super.toLigerUiSelectJson(pageInfo);
	}

	// 添加数据 添加或者修改数据页面 主键为id
	// 如果没有id 不查 如果有查询补上信息
	@RequestMapping(value = "v_syllabus")
	public ModelAndView add(Syllabus syllabus) {
		ModelAndView result = new ModelAndView();
		if (syllabus.getId() != null) {
			syllabus = syllabusService.selectByKey(syllabus.getId());
		}
		result.addObject("syllabus", syllabus);
		return result;
	}

	// 如果有id就更新 没有 就插入
	@RequestMapping(value = "c_syllabus")
	public ModelAndView save(Syllabus syllabus) {
		ModelAndView result = new ModelAndView(redirect_list);
		if (syllabus.getId() != null) {
			syllabusService.updateNotNull(syllabus);
		} else {
			// 会保存null 值
			syllabusService.insertSelective(syllabus);
		}
		return result;
	}
	
	//患者查询回顾课程表
	@RequestMapping(value = "slectSyllabusList")
	public ModelAndView slectSyllabusList(Syllabus syllabus){
		ModelAndView result = new ModelAndView("classroom/classroomlist/reviewclassroomlist");
		syllabus.setSyllabusType(1);
		List<Syllabus> syllabusList = syllabusService.selectSyllabusList(syllabus);
		result.addObject("syllabusList", syllabusList);
		return result;
	}
	
	//患者查询课表
	@RequestMapping(value = "slectCourseList")
	public ModelAndView slectCourseList(Syllabus syllabus, HttpServletRequest request){
		ModelAndView result = new ModelAndView("classroom/classroomlist/classroomlist");
		syllabus.setSyllabusType(0);
		Bespoke bespoke = new Bespoke();
		Integer patientId = (Integer) request.getSession().getAttribute("patientId");
		if(patientId != null){
			bespoke.setPatientId(patientId);
		}else{
			bespoke.setPatientId(syllabus.getPatientId());
		}
		System.out.println(request.getSession().getId());
		List<Bespoke> bespokeList = bespokeService.selectBespokeList(bespoke);
		List<Syllabus> syllabusList = syllabusService.selectSyllabusList(syllabus);
		for(int i = 0; i < bespokeList.size(); i++){
			for(int j = 0; j < syllabusList.size(); j++){
				if(bespokeList.get(i).getSyllabusId() == syllabusList.get(j).getId()){
					syllabusList.get(j).setType(1);
					break;
				}
			}
		}
		result.addObject("syllabusList", syllabusList);
		return result;
	}
	
	//医生查询自己的课程表
	@RequestMapping(value = "doctorSelectSyllabs")
	public ModelAndView doctorSelectSyllabs(Syllabus syllabus){
		ModelAndView result = new ModelAndView("enrollment/doctorin/doctorclass");
		syllabus.setSyllabusType(0);
		List<Syllabus> doctorSyllabsList = syllabusService.doctorSelectSyllabs(syllabus);
		syllabus.setSyllabusType(1);
		List<Syllabus> doctorSyllabsList1 = syllabusService.doctorSelectSyllabs(syllabus);
		result.addObject("doctorSyllabsList", doctorSyllabsList);
		result.addObject("doctorSyllabsList1", doctorSyllabsList1);
		return result;
	}
	
	//医生进入课堂页面
	@RequestMapping(value = "doctorEntrySyllabus")
	public ModelAndView doctorEntrySyllabus(Syllabus syllabus){
		ModelAndView result = new ModelAndView("classroom/classreplay/doctorsyllabus");
		syllabus = syllabusService.selectSyllabus(syllabus);
		result.addObject("syllabus", syllabus);
		return result;
	}
	
	// 删除数据
	@RequestMapping("d_syllabus")
	public String delete(Integer id) {
		syllabusService.delete(id);
		return redirect_list;
	}
	
	//进入课程页面
	@RequestMapping(value = "selectSyllabus")
	public ModelAndView selectSyllabus(Syllabus syllabus){
		ModelAndView result = new ModelAndView("classroom/classreplay/classplay");
		ModelAndView result1 = new ModelAndView("classroom/classreplay/classreplay");
		if(syllabus.getId() != null && syllabus.getSyllabusType() != 1){
			syllabus = syllabusService.selectSyllabus(syllabus);
			result.addObject("syllabus", syllabus);
			return result;
		}else{
			syllabus = syllabusService.selectSyllabus(syllabus);
			result1.addObject("syllabus", syllabus);
			return result1;
		}
	}
	
	//查询正在讲课课程
	@RequestMapping(value="selectStartSyllabus")
	public ModelAndView selectStartSyllabus(Syllabus syllabus){
		ModelAndView result = new ModelAndView("classroom/classroom/classroom");
		List<Syllabus> syllabusList = syllabusService.selectStartSyllabus(syllabus);
		result.addObject("syllabusList", syllabusList);
		return result;
	}
	
}